/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.privat24.api.model;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.privat24.api.misc.TagAttributesReader;
import org.xml.sax.SAXException;

/**
 *
 * @author dezmond
 */
public final class Privat24Response {
    
    public final HashMap<String,String> model = new HashMap<String, String>();
    
    public Privat24Response(String xmlResponse){
        
        try {
            model.putAll(getAttributesMap(xmlResponse));
        } catch (SAXException ex) {
            Logger.getLogger(Privat24Response.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(Privat24Response.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Privat24Response.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    public String getParameter(String parameter){
        return model.get(parameter);
    }
    
    public HashMap<String, String> getAttributesMap(String resultXml) throws SAXException, ParserConfigurationException, IOException{

        TagAttributesReader handler = new TagAttributesReader(model);
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setValidating(false);

        SAXParser parser = factory.newSAXParser();
        parser.parse(new ByteArrayInputStream(resultXml.getBytes()), handler);
        
        return handler.getParsedAttributesMap();

    }
    
    

}
